<?php
namespace app\common\builder\form\traits;



use app\common\Arr;
use app\common\Str;

/**
 * 解析表单字段属性
 * Trait ParseItem
 * @package app\common\builder\form\traits
 */
trait ParseItem
{
    /**
     * 属性解析列表
     * @var array
     */
    protected $parseAttrsList = ['attrs', 'labelTag', 'isHidden', 'tip', 'fetch'];

    /**
     * 解析attrs属性  -- 生成到标签上属性
     * @param $item
     * @return mixed
     */
    protected function parseAttrsAttr($item)
    {
        $defaultAttrs = $this->itemForAttrs[$item['elem']] ?? [];
        // value
        if (isset($item['attrs']['value'])) {
            $this->setItemValue($item['field'], $item['attrs']['value']);
        }
        // 把表单的值 放入 标签属性的value中
        if (isset($this->data[$item['field']])) {
            $item['attrs']['value'] = !is_json($this->data[$item['field']]) ? $this->data[$item['field']]: json_decode($this->data[$item['field']], true);
        }

        // name
        if (empty($item['attrs']['name'])) {
            $item['attrs']['name'] = $item['field'];
        }

        // id
        if (empty($item['attrs']['id'])) {
            $item['attrs']['id'] = 'id_' . $item['field'];
        }

        // class
        $item['attrs']['class'] = $item['attrs']['class'] ?? '';
        if (isset($defaultAttrs['class'])) {
            $item['attrs']['class'] .= ' ' . $defaultAttrs['class'];
            unset($defaultAttrs['class']);
        }
        $item['attrs']['class'] .= ' form-item form-item-' . $item['elem'] . ' form-item-field-' . $item['field'];

        // 合并类型默认的标签属性
        $item['attrs'] = array_merge($defaultAttrs, $item['attrs']);

        return $item;
    }

    /**
     * 解析生成label标签
     * @param $item
     * @return mixed
     */
    protected function parseLabelTagAttr($item)
    {
        $item['labelTag'] = sprintf(
            '<label for="%s" class="layui-form-label"  title="%s">%s：</label>',
            $item['attrs']['id'] ?? '',
            $item['label'] ??  Str::studly($item['field']),
            $item['label'] ??  Str::studly($item['field'])
        );
        return $item;
    }

    /**
     * 解析模板路径
     * @param $item
     * @return mixed
     */
    protected function parseFetchAttr($item)
    {
        // 完全自定义模板路径
        if (!empty($item['fetch']) && is_file($item['fetch'])) {
            return $item;
        }
        $fetch = empty($item['fetch']) ? $item['elem'] : $item['fetch'];
        // 每个应用可以单独设置每种表单类型模板
        $item['fetch'] = app()->getAppPath() . 'view' .
            DIRECTORY_SEPARATOR . 'builder' .
            DIRECTORY_SEPARATOR . 'form' .
            DIRECTORY_SEPARATOR . 'items' .
            DIRECTORY_SEPARATOR . $fetch . '.html';
        // 默认模板
        if (!is_file($item['fetch'])) {
            $item['fetch'] = app()->getBasePath() . 'common' .
                DIRECTORY_SEPARATOR . 'builder' .
                DIRECTORY_SEPARATOR . 'form' .
                DIRECTORY_SEPARATOR . 'items' .
                DIRECTORY_SEPARATOR . $fetch . '.html';
        }
        return $item;
    }

    /**
     * 提示信息
     * @param $item
     * @return mixed
     */
    public function parseTipAttr($item)
    {
        $item['tip'] = $item['tip'] ?? '';
        return $item;
    }

    /**
     * 渲染表单最终html结构
     * @param $item
     * @return mixed
     */
    public function parseHtmlAttr($item)
    {
        if (!empty($item['html'])) {
            return $item['html'];
        }
        if (empty($item['fetch'])) {
            return '';
        }
        return $this->fetch($item['fetch'], $item);
    }
}